package com.bookmanager.www.controller;

import com.bookmanager.www.common.utils.Result;
import com.bookmanager.www.entity.FlBook;
import com.bookmanager.www.mapper.FlBookMapper;
import com.bookmanager.www.service.SearchService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.IOException;
import java.time.Duration;
import java.time.Instant;
import java.time.LocalDate;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 *
 * @author: 风离
 * @Date: 2021/09/30/17:40
 * @Description:
 */
@RestController
@Api(tags="查询图书管理")
@RequestMapping("/fl-searchBook")
public class SearchBookController {

    @Resource
    FlBookMapper flBookMapper;

    @Resource
    SearchService searchService;

    @GetMapping("/searchBooksByElastSearch")
    @ApiOperation(value = "ES支持四种找书方式 ISBN 图书名 出版社 作者",notes = "searchType值为1 ： isbn找书 " +
            "2： 出版社找书 3：作者找书 4：图书名找书" )
    Result searchBooksByElasticSearch(@RequestParam("searchType")Integer searchType,@RequestParam("keywords")String keywords,
    @RequestParam("pageSize")Integer pageSize,@RequestParam("page")Integer pageNo
    ) throws Exception {
//        每次查询之前都更新一下ES数据索引 确保获取到的数据是最新的 定期更新即可
//        searchService.insertBatchBooksIntoES();

        List<Map<String, Object>> list = searchService.searchBooks(searchType, keywords, pageSize, pageNo);

        return Result.succ("查找图书信息成功！！",list);
    }

    @GetMapping("/searchBooksByBookName")
    @ApiOperation(value="图书名找书")
    Result searchBooksByBookName(@RequestParam("bookName")String bookName)
    {
        Instant inst1 = Instant.now();

        List<FlBook> flBookList = flBookMapper.searchBooksByBookName(bookName);
        if (flBookList!=null)
        {
            Instant inst3 = Instant.now();

            System.err.println("查询所需时间为(单位毫秒)"+ Duration.between(inst1, inst3).toMillis());
            return Result.succ("查询图书信息成功！",flBookList);
        }

        return Result.fail("查询图书信息失败 ","未找到该本图书");
    }

    @GetMapping("/searchBookByISBN")
    @ApiOperation(value="ISBN找书")
    Result searchBookByISBN(@RequestParam("ISBN")String isbn)
    {
        Instant inst1 = Instant.now();
        FlBook book = flBookMapper.selectBookByISBN(isbn);
        if(book!=null)
        {
            Instant inst3 = Instant.now();

            System.err.println("查询所需时间为(单位毫秒)"+ Duration.between(inst1, inst3).toMillis());
            return Result.succ("查询图书成功！",book);
        }

        return Result.fail("数据库中无此书资源，联系管理员添加该图书~","");
    }

    @GetMapping("/searchBooksByPublishing")
    @ApiOperation(value="出版社找书")
    Result searchBooksByPublishing(@RequestParam("publishing")String publishing)
    {
        Instant inst1 = Instant.now();
        List<FlBook> flBookList=flBookMapper.searchBooksByPublishing(publishing);
        if(flBookList!=null)
        {
            Instant inst3 = Instant.now();

            System.err.println("查询所需时间为(单位毫秒)"+ Duration.between(inst1, inst3).toMillis());
            return Result.succ("查询图书信息成功！！",flBookList);
        }
        return Result.fail("无此出版社！！！","");
    }


}
